package com.budwk.app.fetchdata.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.base.utils.CoordinateUtils;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.fetchdata.models.FetchData_volunteer;
import com.budwk.app.fetchdata.services.FetchdataVolunteerService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.dao.util.cri.Static;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

@IocBean
@At("/platform/fetchdata/spider/volunteer/team")
@ApiDefinition(tag = "志愿者团队数据表")
@Slf4j
public class FetchdataVolunteerController{
    @Inject
    private FetchdataVolunteerService wxSpiderVolunteerTeamService;

    @Inject
    Dao dao;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询志愿者团队数据表")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = FetchData_volunteer.class
    )
    @ApiResponses(
    implementation = FetchData_volunteer.class
    )
    @SaCheckPermission("fetchdata.spider.volunteer.team")
    public Result<?> list(@Param("..")FetchData_volunteer bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) throws ParseException {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getTeamname() !=null && Strings.isNotBlank(bean.getTeamname().toString())) {
            cnd.and("teamname","like","%"+bean.getTeamname()+"%");
        }
        
        if (bean.getServicearea() !=null && Strings.isNotBlank(bean.getServicearea().toString())) {
            cnd.and("servicearea","like","%"+bean.getServicearea()+"%");
        }
        
        if (bean.getServicetype() !=null && Strings.isNotBlank(bean.getServicetype().toString())) {
            cnd.and("servicetype","like","%"+bean.getServicetype()+"%");
        }
        
        if (bean.getUnittype() !=null && Strings.isNotBlank(bean.getUnittype().toString())) {
            cnd.and("unittype","like","%"+bean.getUnittype()+"%");
        }
        
        if (bean.getContactname() !=null && Strings.isNotBlank(bean.getContactname().toString())) {
            cnd.and("contactname","like","%"+bean.getContactname()+"%");
        }
        
        if (bean.getLandlinephone() !=null && Strings.isNotBlank(bean.getLandlinephone().toString())) {
            cnd.and("landlinephone","like","%"+bean.getLandlinephone()+"%");
        }
        
        if (bean.getContactphone() !=null && Strings.isNotBlank(bean.getContactphone().toString())) {
            cnd.and("contactphone","like","%"+bean.getContactphone()+"%");
        }
        
        if (bean.getContactemail() !=null && Strings.isNotBlank(bean.getContactemail().toString())) {
            cnd.and("contactemail","like","%"+bean.getContactemail()+"%");
        }
        
        if (bean.getAddress() !=null && Strings.isNotBlank(bean.getAddress().toString())) {
            cnd.and("address","like","%"+bean.getAddress()+"%");
        }
        
        if (bean.getProvince() !=null && Strings.isNotBlank(bean.getProvince().toString())) {
            cnd.and("province","like","%"+bean.getProvince()+"%");
        }
        
        if (bean.getCity() !=null && Strings.isNotBlank(bean.getCity().toString())) {
            cnd.and("city","like","%"+bean.getCity()+"%");
        }
        
        if (bean.getDistrict() !=null && Strings.isNotBlank(bean.getDistrict().toString())) {
            cnd.and("district","like","%"+bean.getDistrict()+"%");
        }
        
        if (bean.getStreet() !=null && Strings.isNotBlank(bean.getStreet().toString())) {
            cnd.and("street","like","%"+bean.getStreet()+"%");
        }
        
        if (bean.getDetailedaddress() !=null && Strings.isNotBlank(bean.getDetailedaddress().toString())) {
            cnd.and("detailedaddress","like","%"+bean.getDetailedaddress()+"%");
        }
        
        if (bean.getLongitude() !=null && Strings.isNotBlank(bean.getLongitude().toString())) {
            cnd.and("longitude","like","%"+bean.getLongitude()+"%");
        }
        
        if (bean.getLatitude() !=null && Strings.isNotBlank(bean.getLatitude().toString())) {
            cnd.and("latitude","like","%"+bean.getLatitude()+"%");
        }
        
        if (bean.getScale() !=null && Strings.isNotBlank(bean.getScale().toString())) {
            cnd.and("scale","like","%"+bean.getScale()+"%");
        }
        
        if (bean.getOfficialstaff() !=null && Strings.isNotBlank(bean.getOfficialstaff().toString())) {
            cnd.and("officialstaff","like","%"+bean.getOfficialstaff()+"%");
        }
        
        if (bean.getRegistrationdate() !=null && Strings.isNotBlank(bean.getRegistrationdate().toString())) {
            String[] date=bean.getRegistrationdate().toString().substring(2,21).replace('"',' ').replace('[',' ').replace(']',' ').replaceAll(" ","").split(",");

            //cnd.where(new Static(" 'registrationdate' between '"+date[0]+"' and  '"+date[1]+"'"));

            cnd.and("registrationdate","between",new Object[]{bean.getRegistrationdate().toString().substring(2,21),bean.getRegistrationdate().toString().substring(25,44)});
            //cnd.and("registrationdate","between",bean.getRegistrationdate().toString().substring(2,21)+" "+"and"+" "+bean.getRegistrationdate().toString().substring(25,44));
        }
        
        if (bean.getContactnumber() !=null && Strings.isNotBlank(bean.getContactnumber().toString())) {
            cnd.and("contactnumber","like","%"+bean.getContactnumber()+"%");
        }
        
        if (bean.getHowjoin() !=null && Strings.isNotBlank(bean.getHowjoin().toString())) {
            cnd.and("howjoin","like","%"+bean.getHowjoin()+"%");
        }
        
        if (bean.getEstablishmentdate() !=null && Strings.isNotBlank(bean.getEstablishmentdate().toString())) {
            cnd.and("establishmentdate","like","%"+bean.getEstablishmentdate().substring(0,10)+"%");
        }
        
        if (bean.getContactorganization() !=null && Strings.isNotBlank(bean.getContactorganization().toString())) {
            cnd.and("contactorganization","like","%"+bean.getContactorganization()+"%");
        }
        
        if (bean.getRegistrationauthority() !=null && Strings.isNotBlank(bean.getRegistrationauthority().toString())) {
            cnd.and("registrationauthority","like","%"+bean.getRegistrationauthority()+"%");
        }
        
        if (bean.getTeamprofile() !=null && Strings.isNotBlank(bean.getTeamprofile().toString())) {
            cnd.and("teamprofile","like","%"+bean.getTeamprofile()+"%");
        }
        
        if (bean.getProjectname() !=null && Strings.isNotBlank(bean.getProjectname().toString())) {
            cnd.and("projectname","like","%"+bean.getProjectname()+"%");
        }
        
        if (bean.getCreatedate() !=null && Strings.isNotBlank(bean.getCreatedate().toString())) {
            cnd.and("createdate","like","%"+bean.getCreatedate()+"%");
        }

        if (bean.getSoucechannel() !=null && Strings.isNotBlank(bean.getSoucechannel().toString())) {
            cnd.and("soucechannel","like","%"+bean.getSoucechannel()+"%");
        }

        if (bean.getEntername() !=null && Strings.isNotBlank(bean.getEntername().toString())) {
            cnd.and("entername","like","%"+bean.getEntername()+"%");
        }

        if (bean.getEnterphone() !=null && Strings.isNotBlank(bean.getEnterphone().toString())) {
            cnd.and("enterphone","like","%"+bean.getEnterphone()+"%");
        }

        if (bean.getEntertime() !=null && Strings.isNotBlank(bean.getEntertime().toString())) {
            Instant instant = bean.getEntertime().toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String startdateStr = localDateTime.format(fmt);
            String enddateStr = localDateTime.plusDays(1).format(fmt);

            cnd.and("entertime","between",new Object[]{startdateStr,enddateStr});
        }
        
        return Result.data(wxSpiderVolunteerTeamService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增志愿者团队数据表")
    @ApiFormParams(
        implementation = FetchData_volunteer.class
    )
    @ApiResponses
    @SLog(tag = "新增志愿者团队数据表", value = "${args[0].id}")
    @SaCheckPermission("fetchdata.spider.volunteer.team.create")
    public Result<?> create(@Param("..")FetchData_volunteer wxSpiderVolunteerTeam, HttpServletRequest req) {
        wxSpiderVolunteerTeam.setCreatedBy(SecurityUtil.getUserId());
        wxSpiderVolunteerTeamService.insert(wxSpiderVolunteerTeam);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改志愿者团队数据表")
    @ApiFormParams(
        implementation = FetchData_volunteer.class
    )
    @ApiResponses
    @SLog(tag = "修改志愿者团队数据表", value = "${args[0].id}")
    @SaCheckPermission("fetchdata.spider.volunteer.team.update")
    public Result<?> update(@Param("..")FetchData_volunteer wxSpiderVolunteerTeam, HttpServletRequest req) {
        wxSpiderVolunteerTeam.setUpdatedBy(SecurityUtil.getUserId());
        wxSpiderVolunteerTeamService.updateIgnoreNull(wxSpiderVolunteerTeam);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取志愿者团队数据表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("fetchdata.spider.volunteer.team")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        FetchData_volunteer wxSpiderVolunteerTeam = wxSpiderVolunteerTeamService.fetch(id);
        if (wxSpiderVolunteerTeam == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxSpiderVolunteerTeam);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除志愿者团队数据表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除志愿者团队数据表", value = "${args[0]}")
    @SaCheckPermission("fetchdata.spider.volunteer.team.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        FetchData_volunteer wxSpiderVolunteerTeam = wxSpiderVolunteerTeamService.fetch(id);
        if (wxSpiderVolunteerTeam == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxSpiderVolunteerTeamService.delete(id);
        req.setAttribute("_slog_msg", wxSpiderVolunteerTeam.getId());
        return Result.success();
    }


    @At("/rescueForce")
    @Ok("json")
    @POST
    @ApiOperation(name = "救援力量")
    @ApiFormParams()
    @SLog(tag = "救援力量", value = "${args[0]}")
    public Result rescueForce() {
        Sql zydrssql= Sqls.create("select count(*) from wx_spider_volunteer_team where (province != '' or province is not null)");
        int zyzdw=wxSpiderVolunteerTeamService.count(zydrssql);

        Sql sql= Sqls.create("select sum(cast(officialstaff as float8)) from wx_spider_volunteer_team");
        int zyzzrs=wxSpiderVolunteerTeamService.count(sql);

        Cnd cnd = Cnd.NEW();
        cnd.and("servicetype", "like", "%应急救援%");
        /*SqlExpressionGroup e1 = Cnd.exps("province", "!=", "''").or("province", "is not", null);
        cnd.and(e1);*/
        int jydw=wxSpiderVolunteerTeamService.count(cnd);

        Sql jydwsql= Sqls.create("select sum(cast(officialstaff as float8)) from wx_spider_volunteer_team where servicetype like '%应急救援%'");
        int jydwzrs=wxSpiderVolunteerTeamService.count(jydwsql);

        Cnd jdbcnd = Cnd.NEW();
        jdbcnd.and("unittype", "like", "%街道办事处%");
        int jdb=wxSpiderVolunteerTeamService.count(jdbcnd);

        Cnd sqyycnd = Cnd.NEW();
        sqyycnd.and("unittype", "like", "%卫生服务中心%");
        int sqyy=wxSpiderVolunteerTeamService.count(sqyycnd);

        Cnd pcscnd = Cnd.NEW();
        pcscnd.and("unittype", "like", "%派出所%");
        int pcs=wxSpiderVolunteerTeamService.count(pcscnd);

        Map map=new HashMap();
        map.put("zyzdw",zyzdw);//志愿者队伍数量
        map.put("zyzzrs",zyzzrs);//志愿者队伍总人数
        map.put("jydw",jydw);//救援队伍数量
        map.put("jydwzrs",jydwzrs);//救援队伍总人数
        map.put("jdb",jdb);//街道办
        map.put("sqyy",sqyy);//社区医院
        map.put("pcs",pcs);//派出所

        Result result=Result.NEW();
        result.addData(map);


        return  result;
    }


    @At("/getProvinceCount")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取省份信息条数")
    @ApiFormParams()
    @SLog(tag = "获取省份信息条数", value = "${args[0]}")
    public Result getProvinceCount(@Param("type")Object type,@Param("totalChineseProvinces")Object totalChineseProvinces){


        String str = JSONObject.toJSONString(totalChineseProvinces);
        List<JSONObject> models = JSONObject.parseArray(str, JSONObject.class);

        for (int j=0;j<models.size();j++) {
            Cnd cnd = Cnd.NEW();
            cnd.and("teamname", "like", "%"+type.toString().substring(type.toString().length()-1,type.toString().length())+"%");
            cnd.and("province", "like", "%"+models.get(j).get("name")+"%");
            //cnd.having(Cnd.wrap("count(province)>0"));
            Integer province = wxSpiderVolunteerTeamService.count(cnd);

            models.get(j).put("count",province);

        }


        Result result=Result.NEW();
        result.addData(models);

        return result;


    }


    @At("/getCountAll")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取省市区获取信息条数")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "name", description = "名字",example = "贵州"),
                    @ApiFormParam(name = "typeaddress", description = "省市区",example = "省"),
                    @ApiFormParam(name = "type", description = "类型",example = "街道办")
            }
    )
    @SLog(tag = "获取省市区获取信息条数", value = "${args[0]}")
    public Result getCountAll(@Param("name")Object name,@Param("typeaddress")Object typeaddress,@Param("type")Object type) {
        Result result=Result.NEW();


        if (type.toString().contains("志愿")){
            if (typeaddress.toString().contains("省")){
                String sqls=" SELECT count(city),city,province from wx_spider_volunteer_team where province like '%"+name+"%'  GROUP BY city,province HAVING count(city)>0";
                Sql sql = Sqls.create(sqls);

                List<JSONObject> list = new ArrayList<JSONObject>();
                sql.setCallback(new SqlCallback() {
                    @Override
                    public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                        while (rs.next()) {
                            Map location= CoordinateUtils.getLatAndLngByAddrforTengxun(rs.getString("city"));
                            JSONObject obj = new JSONObject();
                            obj.put("count", rs.getString("count"));
                            obj.put("city", rs.getString("city"));
                            obj.put("type", typeaddress.toString());
                            obj.put("latitude", location.get("lat"));
                            obj.put("longitude", location.get("lng"));
                            obj.put("id", location.get("code"));
                            obj.put("notclass", "not");

                            list.add(obj);
                        }
                        return list;
                    }
                });//实体回调，不构建实体类
                Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类，查一下就知道了
                sql.setEntity(entity);
                dao.execute(sql);
                List<Record> map = sql.getList(Record.class);
                result.addData(list);
                return result;

            }
            if (typeaddress.toString().contains("市")){
                String sqls=" SELECT count(district),district,city from wx_spider_volunteer_team where city like '%"+name+"%'  GROUP BY district,city HAVING count(district)>0";
                Sql sql = Sqls.create(sqls);

                List<JSONObject> list = new ArrayList<JSONObject>();
                sql.setCallback(new SqlCallback() {
                    @Override
                    public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                        while (rs.next()) {
                            Map location= CoordinateUtils.getLatAndLngByAddrforTengxun(rs.getString("district"));
                            JSONObject obj = new JSONObject();
                            obj.put("count", rs.getString("count"));
                            obj.put("district", rs.getString("district"));
                            obj.put("type", typeaddress.toString());
                            obj.put("latitude", location.get("lat"));
                            obj.put("longitude", location.get("lng"));
                            obj.put("id", location.get("code"));
                            obj.put("notclass", "not");


                            list.add(obj);
                        }
                        return list;
                    }
                });//实体回调，不构建实体类
                Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类，查一下就知道了
                sql.setEntity(entity);
                dao.execute(sql);
                List<Record> map = sql.getList(Record.class);
                result.addData(list);
                return result;
            }

            if (typeaddress.toString().contains("区")||typeaddress.toString().contains("县")){
                String sqls=" SELECT * from wx_spider_volunteer_team a where district like '%"+name+"%' ";
                Sql sql = Sqls.create(sqls);

                List<JSONObject> list = new ArrayList<JSONObject>();
                sql.setCallback(new SqlCallback() {
                    @Override
                    public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                        while (rs.next()) {
                            JSONObject obj = new JSONObject();
                            obj.put("name", rs.getString("teamname"));
                            obj.put("servicearea", rs.getString("servicearea"));
                            obj.put("servicetype", rs.getString("servicetype"));
                            obj.put("unittype", rs.getString("unittype"));
                            obj.put("street", rs.getString("street"));

                            if (rs.getString("street")==null||rs.getString("street").isEmpty()||rs.getString("street").equals("")){
                                obj.put("address", rs.getString("province")+rs.getString("city")+rs.getString("district"));
                            }else {
                                obj.put("address", rs.getString("province")+rs.getString("city")+rs.getString("district")+rs.getString("street"));

                            }
                            obj.put("phone", rs.getString("contactphone"));
                            obj.put("address", rs.getString("address"));
                            obj.put("longitude", rs.getString("longitude"));
                            obj.put("latitude", rs.getString("latitude"));
                            obj.put("teamprofile", rs.getString("teamprofile"));
                            obj.put("officialstaff", rs.getString("officialstaff"));
                            obj.put("howjoin", rs.getString("howjoin"));

                            obj.put("type", typeaddress.toString());
                            obj.put("notclass", "is");

                            list.add(obj);
                        }
                        return list;
                    }
                });//实体回调，不构建实体类
                Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类，查一下就知道了
                sql.setEntity(entity);
                dao.execute(sql);
                List<Record> map = sql.getList(Record.class);
                result.addData(list);
                return result;
            }


        }if(type.toString().contains("救援")){
            if (typeaddress.toString().contains("省")){
                String sqls=" SELECT count(city),city,province from wx_spider_volunteer_team where province like '%"+name+"%' and servicetype like '%"+type+"%' GROUP BY city,province HAVING count(city)>0";
                Sql sql = Sqls.create(sqls);

                List<JSONObject> list = new ArrayList<JSONObject>();
                sql.setCallback(new SqlCallback() {
                    @Override
                    public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                        while (rs.next()) {
                            Map location= CoordinateUtils.getLatAndLngByAddrforTengxun(rs.getString("city"));
                            JSONObject obj = new JSONObject();
                            obj.put("count", rs.getString("count"));
                            obj.put("city", rs.getString("city"));
                            obj.put("type", typeaddress.toString());
                            obj.put("latitude", location.get("lat"));
                            obj.put("longitude", location.get("lng"));
                            obj.put("id", location.get("code"));
                            obj.put("notclass", "not");

                            list.add(obj);
                        }
                        return list;
                    }
                });//实体回调，不构建实体类
                Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类，查一下就知道了
                sql.setEntity(entity);
                dao.execute(sql);
                List<Record> map = sql.getList(Record.class);
                result.addData(list);
                return result;

            }
            if (typeaddress.toString().contains("市")){
                String sqls=" SELECT count(district),district,city from wx_spider_volunteer_team where city like '%"+name+"%' and servicetype like '%"+type+"%'  GROUP BY district,city HAVING count(district)>0";
                Sql sql = Sqls.create(sqls);

                List<JSONObject> list = new ArrayList<JSONObject>();
                sql.setCallback(new SqlCallback() {
                    @Override
                    public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                        while (rs.next()) {
                            Map location= CoordinateUtils.getLatAndLngByAddrforTengxun(rs.getString("district"));
                            JSONObject obj = new JSONObject();
                            obj.put("count", rs.getString("count"));
                            obj.put("district", rs.getString("district"));
                            obj.put("type", typeaddress.toString());
                            obj.put("latitude", location.get("lat"));
                            obj.put("longitude", location.get("lng"));
                            obj.put("id", location.get("code"));
                            obj.put("notclass", "not");

                            list.add(obj);
                        }
                        return list;
                    }
                });//实体回调，不构建实体类
                Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类，查一下就知道了
                sql.setEntity(entity);
                dao.execute(sql);
                List<Record> map = sql.getList(Record.class);
                result.addData(list);
                return result;
            }

            if (typeaddress.toString().contains("区")||typeaddress.toString().contains("县")){
                String sqls=" SELECT * from wx_spider_volunteer_team a where district like '%"+name+"%'  and servicetype like '%"+type+"%'";
                Sql sql = Sqls.create(sqls);

                List<JSONObject> list = new ArrayList<JSONObject>();
                sql.setCallback(new SqlCallback() {
                    @Override
                    public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                        while (rs.next()) {
                            JSONObject obj = new JSONObject();
                            obj.put("name", rs.getString("teamname"));
                            obj.put("servicearea", rs.getString("servicearea"));
                            obj.put("servicetype", rs.getString("servicetype"));
                            obj.put("unittype", rs.getString("unittype"));
                            obj.put("street", rs.getString("street"));

                            if (rs.getString("street")==null||rs.getString("street").isEmpty()||rs.getString("street").equals("")){
                                obj.put("address", rs.getString("province")+rs.getString("city")+rs.getString("district"));
                            }else {
                                obj.put("address", rs.getString("province")+rs.getString("city")+rs.getString("district")+rs.getString("street"));

                            }
                            obj.put("phone", rs.getString("contactphone"));
                            obj.put("contactname", rs.getString("contactname"));
                            obj.put("address", rs.getString("address"));
                            obj.put("longitude", rs.getString("longitude"));
                            obj.put("latitude", rs.getString("latitude"));
                            obj.put("teamprofile", rs.getString("teamprofile"));
                            obj.put("officialstaff", rs.getString("officialstaff"));
                            obj.put("howjoin", rs.getString("howjoin"));


                            obj.put("type", typeaddress.toString());
                            obj.put("notclass", "is");

                            list.add(obj);
                        }
                        return list;
                    }
                });//实体回调，不构建实体类
                Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类，查一下就知道了
                sql.setEntity(entity);
                dao.execute(sql);
                List<Record> map = sql.getList(Record.class);
                result.addData(list);
                return result;
            }

        }

        else{
        if (typeaddress.toString().contains("省")){
            String sqls=" SELECT count(city),city,province from wx_spider_volunteer_team where province like '%"+name+"%' and unittype like '%"+type+"%' GROUP BY city,province HAVING count(city)>0";
            Sql sql = Sqls.create(sqls);

            List<JSONObject> list = new ArrayList<JSONObject>();
            sql.setCallback(new SqlCallback() {
                @Override
                public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                    while (rs.next()) {
                        Map location= CoordinateUtils.getLatAndLngByAddrforTengxun(rs.getString("city"));
                        JSONObject obj = new JSONObject();
                        obj.put("count", rs.getString("count"));
                        obj.put("city", rs.getString("city"));
                        obj.put("type", typeaddress.toString());
                        obj.put("latitude", location.get("lat"));
                        obj.put("longitude", location.get("lng"));
                        obj.put("id", location.get("code"));
                        obj.put("notclass", "not");

                        list.add(obj);
                    }
                    return list;
                }
            });//实体回调，不构建实体类
            Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类，查一下就知道了
            sql.setEntity(entity);
            dao.execute(sql);
            List<Record> map = sql.getList(Record.class);
            result.addData(list);
            return result;

        }
        if (typeaddress.toString().contains("市")){
            String sqls=" SELECT count(district),district,city from wx_spider_volunteer_team where city like '%"+name+"%' and unittype like '%"+type+"%'  GROUP BY district,city HAVING count(district)>0";
            Sql sql = Sqls.create(sqls);

            List<JSONObject> list = new ArrayList<JSONObject>();
            sql.setCallback(new SqlCallback() {
                @Override
                public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                    while (rs.next()) {
                        Map location= CoordinateUtils.getLatAndLngByAddrforTengxun(rs.getString("district"));
                        JSONObject obj = new JSONObject();
                        obj.put("count", rs.getString("count"));
                        obj.put("district", rs.getString("district"));
                        obj.put("type", typeaddress.toString());
                        obj.put("latitude", location.get("lat"));
                        obj.put("longitude", location.get("lng"));
                        obj.put("id", location.get("code"));
                        obj.put("notclass", "not");

                        list.add(obj);
                    }
                    return list;
                }
            });//实体回调，不构建实体类
            Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类，查一下就知道了
            sql.setEntity(entity);
            dao.execute(sql);
            List<Record> map = sql.getList(Record.class);
            result.addData(list);
            return result;
        }

        if (typeaddress.toString().contains("区")||typeaddress.toString().contains("县")){
            String sqls=" SELECT * from wx_spider_volunteer_team a where district like '%"+name+"%'  and unittype like '%"+type+"%'";
            Sql sql = Sqls.create(sqls);

            List<JSONObject> list = new ArrayList<JSONObject>();
            sql.setCallback(new SqlCallback() {
                @Override
                public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                    while (rs.next()) {
                        JSONObject obj = new JSONObject();
                        obj.put("name", rs.getString("teamname"));
                        obj.put("servicearea", rs.getString("servicearea"));
                        obj.put("servicetype", rs.getString("servicetype"));
                        obj.put("unittype", rs.getString("unittype"));
                        obj.put("street", rs.getString("street"));

                        if (rs.getString("street")==null||rs.getString("street").isEmpty()||rs.getString("street").equals("")){
                            obj.put("address", rs.getString("province")+rs.getString("city")+rs.getString("district"));
                        }else {
                            obj.put("address", rs.getString("province")+rs.getString("city")+rs.getString("district")+rs.getString("street"));

                        }
                        obj.put("phone", rs.getString("contactphone"));
                        obj.put("address", rs.getString("address"));
                        obj.put("longitude", rs.getString("longitude"));
                        obj.put("latitude", rs.getString("latitude"));
                        obj.put("teamprofile", rs.getString("teamprofile"));
                        obj.put("officialstaff", rs.getString("officialstaff"));
                        obj.put("howjoin", rs.getString("howjoin"));
                        obj.put("type", typeaddress.toString());
                        obj.put("notclass", "is");

                        list.add(obj);
                    }
                    return list;
                }
            });//实体回调，不构建实体类
            Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类，查一下就知道了
            sql.setEntity(entity);
            dao.execute(sql);
            List<Record> map = sql.getList(Record.class);
            result.addData(list);
            return result;
        }

        }

        return  result;
    }



}
